package com.feiwo.nonego.rpc.reflect.javassist;

import com.feiwo.nonego.rpc.reflect.api.ReflectInvoker;
import com.feiwo.nonego.rpc.spi.annotation.SPIClass;
import javassist.util.proxy.ProxyFactory;

/**
 *
 *
 * @author feiwo 2025-08-17
 */
@SPIClass
public class JavassistReflectInvoker implements ReflectInvoker {

    @Override
    public Object invoke(Object serviceBean, Class<?> serviceClass, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Throwable {
        var proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(serviceClass);
        var implClass = proxyFactory.createClass();
        var method = implClass.getMethod(methodName, parameterTypes);
        method.setAccessible(true);
        return method.invoke(implClass.getDeclaredConstructor().newInstance(), parameters);
    }
}
